package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.mapper.SkuMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {
    @Autowired
    private AlbumMapper mapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SpuMapper spuMapper;

    public void addNew(AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册的业务】，参数：{}",albumAddNewDTO);
        String name = albumAddNewDTO.getName();
        int countByName = mapper.countByName(name);
        if (countByName > 0){
            String message = "添加相册失败，相册名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewDTO,album);
        int insertRows = mapper.insert(album);
        if (insertRows != 1){
            String message = "添加类相册失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    public void deleteById(Long id) {
        if (mapper.getStandardById(id) == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"抱歉，该相册不存在哦！");
        }else if (pictureMapper.countByAlbumId(id) > 0){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"抱歉，该相册存在关联的图片数据");
        }else if (skuMapper.countByAlbum(id) > 0){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"抱歉，该相册存在关联sku的数据");
        }else if (spuMapper.countByAlbum(id) > 0){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"抱歉，该相册存在关联spu的数据");
        }
        int deleteRows =  mapper.deleteById(id);
        if (deleteRows != 1){
            String message = "删除相册失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    public void updateInfoById(Long id,AlbumUpdateDTO updateDTO) {
        log.debug("开始处理修改相册，请求id是：{},参数是：{}",id,updateDTO);
        if (mapper.getStandardById(id) == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"抱歉，该相册不存在哦！");
        }else if (mapper.countByNameAndNotId(
                updateDTO.getName(), id) > 0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"抱歉，相册名称已存在，请重新输入！");
        }
        Album album = new Album();
        album.setId(id);
        BeanUtils.copyProperties(updateDTO,album);
        log.debug("开始处理修改相册，实际修改id是：{},参数是：{}",id,album);
        int updateRows =  mapper.update(album);
        if (updateRows != 1){
            String message = "更新相册失败，服务器忙，请稍后再次尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    public AlbumStandardVO getStandardById(Long id) {
        AlbumStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"抱歉，该相册不存在！");
        }
        return standardVO;
    }

    public List<AlbumListItemVO> getListItem() {
        return mapper.list();
    }
}
